import time

from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from datetime import datetime


class Base():
    #封装初始化浏览器驱动
    def __init__(self, driver):
        self.driver = driver

    #封装打开浏览器首页
    def ope_url(self,url):
        self.driver.get(url)


    #封装定位元素方法
    def find_element(self, loc):
        try:
            #显示等待
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)

        except Exception as e:
            #时间戳格式
            data_time = datetime.now().strftime('%Y%m%d %H%M%S')
            #截图
            self.driver.save_screenshot(f'err_{data_time}.png')

            print('未定位到元素', e)
    #封装输入方法
    def send_keys(self, loc, info):
        self.find_element(loc).send_keys(info)

    #封装点击方法
    def click(self, loc):
        self.find_element(loc).click()
    #封装断言方法
    def duanyan_text(self, loc):
        return self.find_element(loc).text
    #封装进入frame框架
    def frame(self, loc):
        self.driver.switch_to.frame(self.find_element(loc))
    #封装退出frame框架
    def default_content(self):
        self.driver.switch_to.default_content()
    #封装强制等待时间
    def sllep(self, x):
        time.sleep(x)
    #封装隐式等待时间
    def implicitly_wait(self, x):
        self.driver.implicitly_wait(x)
